home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / MACAPP / PRE_3 / UPROGRES / UPROGRES.P < prev   
Text File  |  1990-08-20  |  4KB  |  151 lines

  1. {
  2.     A simple progress bar indicator for MacApp.  Implemented as a view for
  3.     simplicity's sake.  Hey, it works for me, but comments are welcome.
  4.     
  5.     ⌐ 1990 Brian P. Arnold
  6.     All Rights Reserved.
  7.     
  8.     Heck, go ahead and even use it if you want in commercial applications,
  9.     and even bundle it in other MacApp source code whereby you'd be making
  10.     a profit and I wouldn't.  I don't care.
  11.  
  12.     HOW TO USE:
  13.         Create the view using whatever method you prefer: I prefer ViewEdit.
  14.         Call IProgressView after getting a reference to the view.
  15.         Call IncrementProgress repeatedly to automatically increment progress
  16.         ("smooth progress"), or call DoProgress to alter the current value.
  17.         Note that the view relies on your processing update events to draw
  18.         accurately, and it expects silly things like calling IncrementProgress
  19.         exactly the number of times in fMaxValue except if you use DoProgress.
  20.         I don't think it likes decrementing but nobody likes backward "progress"...
  21.         
  22.         If your progress indicator is spring-loaded and isn't used often
  23.         or for long periods, resegment the code appropriately.
  24.     
  25.     TO DO:
  26.         Subclass a timer progress view that uses Ticks and automatic updating.
  27.         Implement as a TControl.
  28. }
  29.  
  30.  
  31. UNIT UProgressView;
  32.  
  33.  
  34. INTERFACE
  35.  
  36. USES
  37.  
  38.     UMacApp;
  39.  
  40. TYPE
  41.  
  42.     TProgressView = OBJECT( TView )
  43.         fCurValue,
  44.         fMaxValue:    INTEGER;
  45.         
  46.         PROCEDURE TProgressView.IProgressView( maxValue: INTEGER );
  47.             { initialize the values }
  48.         
  49.         PROCEDURE TProgressView.Draw( area: Rect );
  50.             OVERRIDE;
  51.             { draw the progress indicator }
  52.         
  53.         PROCEDURE TProgressView.IncrementProgress;
  54.             { increment curvalue, invalidate that part of the view }
  55.         
  56.         PROCEDURE TProgressView.DoProgress( newCurVal: INTEGER );
  57.             { update curvalue, invalidate the difference }
  58.     END;
  59.  
  60.     PROCEDURE InitUProgressView;
  61.         { avoid dead-stripping }
  62.         
  63. IMPLEMENTATION
  64.  
  65. {------------------------------------------------------------------------------------------}    
  66. {$S AInit}
  67.  
  68. PROCEDURE InitUProgressView;
  69. BEGIN
  70.     IF gDeadStripSuppression THEN
  71.         IF Member( TObject( NIL ), TProgressView ) THEN ;
  72. END;
  73.  
  74. {------------------------------------------------------------------------------------------}    
  75. {$S AInit}
  76.  
  77. PROCEDURE TProgressView.IProgressView( maxValue: INTEGER );
  78.     { initialize the values }
  79. BEGIN
  80.     fCurValue := 0;
  81.     fMaxValue := maxValue;
  82. END;
  83.  
  84. {------------------------------------------------------------------------------------------}    
  85. {$S ARes}
  86.  
  87. PROCEDURE TProgressView.Draw( area: Rect );
  88.     OVERRIDE;
  89.     { draw the progress indicator }
  90. VAR
  91.     drawRect:    Rect;
  92. BEGIN
  93.     SELF.GetQDExtent( drawRect );
  94.  
  95. (* draw the border *)
  96.     PenPat( black );
  97.     FrameRect( drawRect );
  98.     
  99. (* figure out the "progress" *)
  100.     InsetRect( drawRect, 1, 1 );
  101.     SetRect( drawRect,    drawRect.left,
  102.                             drawRect.top,
  103.                             ( drawRect.right * fCurValue ) DIV fMaxValue,
  104.                             drawRect.bottom );
  105.     
  106. (* draw the "progress" *)
  107.     PenPat( dkGray );
  108.     PaintRect( drawRect );
  109. END;
  110.  
  111. {------------------------------------------------------------------------------------------}    
  112. {$S ARes}
  113.  
  114. PROCEDURE TProgressView.IncrementProgress;
  115.     { increment curvalue, invalidate that part of the view }
  116. BEGIN
  117.     DoProgress( fCurValue + 1 );
  118. END;
  119.  
  120. {------------------------------------------------------------------------------------------}    
  121. {$S ARes}
  122.  
  123. PROCEDURE TProgressView.DoProgress( newCurVal: INTEGER );
  124.     { update curvalue, invalidate the difference }
  125. VAR
  126.     deltaProgress: Rect;
  127. BEGIN
  128.     IF Focus THEN ;
  129.     GetQDExtent( deltaProgress );
  130.     InsetRect( deltaProgress, 1, 1 );
  131.     
  132. (* set left edge to current value position *)
  133.     deltaProgress.left := ( deltaProgress.right * fCurValue ) DIV fMaxValue;
  134.  
  135.     fCurValue := newCurVal;
  136.     IF fCurValue > fMaxValue THEN BEGIN
  137.  
  138.         IF qDebug THEN
  139.             Writeln('TProgressView.DoProgress: tried to set fCurValue > fMaxValue' );
  140.  
  141.         fCurValue := fMaxValue;
  142.     END;
  143.         
  144. (* set right edge to new value position (+1 is assumption on horizontal pensize) *)
  145.     deltaProgress.right := ( ( deltaProgress.right * fCurValue ) DIV fMaxValue) + 1;
  146.  
  147. (* invalidate the difference *)
  148.     InvalidRect( deltaProgress );
  149. END;
  150.  
  151. END.